//
//  DateViewController.m
//  PickerView
//
//  Created by cocoazxj@163.com on 15/4/1.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "DateViewController.h"

@interface DateViewController ()
@property (nonatomic, strong) IBOutlet UITextField *tf_text;
@end

@implementation DateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    
    UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *bbi1 = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(bbilButtonClicked)];
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(bbi2ButtonClicked)];
    tb.items = @[bbi1,bbi,bbi2];
    self.tf_text.inputAccessoryView = tb;
    self.tf_text.inputView = datePicker;
    
    // 给DatePickerView添加一个点击事件
    [datePicker addTarget:self action:@selector(test:) forControlEvents:UIControlEventValueChanged];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)bbilButtonClicked
{
    NSLog(@"bbilButtonClicked");
}

- (void)bbi2ButtonClicked
{
    NSLog(@"bbi2ButtonClicked");
}

// 点击后，获取改变后的值。
- (void)test:(UIDatePicker *)pickerView
{
    NSLog(@"----------%@",pickerView.date);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *string = [formatter stringFromDate:pickerView.date];
    [self.tf_text setText:string];
}
@end
